<?php
// ------------------------------------------------------------------------------------
// Bestand:       Login, loginController.php
// Versie:        1.1
// Projekt:       Cursus MasterClass van Eduvision
// Datum:         24-06-2008
// Auteur(s):     Ronald Obbens
// Email(s):      r.lagendijk@hccnet.nl/ronaldobbens@gmail.com
// Revisie(s):	  1.1 -> extends controller
//
// Beschrijving:  script that regulates the logon	
// ------------------------------------------------------------------------------------
//
require_once('Controller.php');
require_once('DataBase.php');
require_once('User.php');

class loginController extends Controller
{
	/**
	 * index action
	 * 
	 * default action of the loginController
	 * 
	 * shows a loginpage
	 *
	 */
	public function index()
	{
		parent::render('login.tpl');
	}
	
	/**
	 * login action
	 *
	 * confirm user information and set a User object in the session variables
	 */
	public function login()
	{
		$username = strtolower($_POST['username']);
		$password = md5($_POST['password']);
		
		$DB = new DataBase();
		$output = $DB->runDirectQuery("SELECT id FROM Users WHERE username = '" . $username . "'");
		if($output->getData() === null)
		{
			print 'username not availeble';
		}
		else
		{
			$user = new User();
		
			$userid = $output->getData();	
			$userid = $userid[0];
			
			
			$user->setByParentId($userid['id']);
			
			if($password === $user->getPassword())
			{
				$_SESSION['user'] = $user->getId();
				$this->index();
				
				return true;
				
			}
		
			else
			{
				print 'fout wachtwoord';
			}
		}
	}
	
	public function logout()
	{
		unset($_SESSION['user']);
		$this->index();
	}
}
?>